{% extends 'teacher/base.html' %}
{% load staticfiles %}
{% block content %}

        <!-- Page Heading -->
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">
                    Quizzes
                </h1>
                <ol class="breadcrumb">
                    <li>
                        <i class="fa fa-suitcase"></i>  <a href="/teaching">Teaching</a>
                    </li>
                    <li>
                        <i class="fa fa-compass"></i> {{ course.title }}
                    </li>
                    <li class="active">
                        <i class="fa fa-star-half"></i> Quizzes
                    </li>
                </ol>
            </div>
        </div>
        <!-- /.row -->

        <!-- Quizzes -->
        <div id="ajax_quizzes_table"
           name="ajax_quizzes_table"></div>
        <!-- /.row -->

<!-- Modal -->
<div id="ajax_quiz_modal"
   name="ajax_quiz_modal"></div>
<!-- end modal -->

<script>
    /**
     * When the page loads up, get the table data.
     */
    $(document).ready(function(){
        ajax_refresh_quizzes_table();
    });
                  
    function ajax_refresh_quizzes_table()
    {
        var url = 'quizzes_table';
        
        $.ajax( url, {
            data: {
               'csrfmiddlewaretoken': '{{ csrf_token }}',
            },
            type: 'post',
            success: function(result) {
               // success code execution here
               $('#ajax_quizzes_table').html(result); // Update UI.
            },
            error: function(xhr,status,error) {
               // error code here
            },
            complete: function(xhr,status) {
               // completion code here
            }
        });
    }
    
    /**
     *  This function will load up the quiz question creator/editor page.
     */
    function view_quiz(quiz_id)
    {
        $('#view_'+quiz_id+'_btn').prop("disabled", true); // Lock button
        window.location = "quiz/" + quiz_id;
    }

    function ajax_quiz(quiz_id)
    {
        if (quiz_id == 0)
        {
            $('#add_quiz_btn').prop("disabled", true); // Lock button
        }
        else
        {
            $('#edit_'+quiz_id+'_btn').prop("disabled", true); // Lock button
        }
        $.ajax( 'quiz_modal', {
            data: {
                'csrfmiddlewaretoken': '{{ csrf_token }}',
                'quiz_id': quiz_id
            },
            type: 'post',
            success: function(result) {
                // success code execution here-->
                $('#ajax_quiz_modal').html(result); // Update UI.
                $('#quiz_modal').modal();           // Unhide view.
            },
            error: function(xhr,status,error) {
                // error code here
            },
            complete: function(xhr,status) {
               // completion code here
            }
        });
    }

    function ajax_delete_quiz(quiz_id)
    {
        $('#del_'+quiz_id+'_btn').prop("disabled", true); // Lock button
        $.ajax( 'delete_quiz', {
            data: {
                'csrfmiddlewaretoken': '{{ csrf_token }}',
                'quiz_id' : quiz_id
            },
            type: 'post',
            success: function(result) {
                // success code execution here
                if (result.status == 'success')
                {
                    ajax_refresh_quizzes_table();
                }
            },
            error: function(xhr,status,error) {
                // error code here
            },
            complete: function(xhr,status) {
               // completion code here
               $('#del_'+quiz_id+'_btn').prop("disabled", true); // Lock button
            }
        });
    }
</script>

{% endblock content %}
